//-----------------------------------------------------------------------
// <copyright file="UnitTypeDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class UnitTypeDAOBase
    {
        #region Common methods
        public virtual UnitType CreateUnitTypeFromReader(IDataReader reader)
        {
            UnitType item = new UnitType();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UnitTypeId"))) item.UnitTypeId = (long)reader["UnitTypeId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (long)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUnitTypeFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateUnitType methods
            
        public virtual void CreateUnitType(UnitType unitType)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitTypeInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, unitType.Name);
                database.AddInParameter(dbCommand, "@Enable", DbType.Int64, unitType.Enable);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, unitType.Order);
                database.AddOutParameter(dbCommand, "@UnitTypeId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                unitType.UnitTypeId = (long)database.GetParameterValue(dbCommand, "@UnitTypeId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateUnitTypeException, ex);
            }
        }

        #endregion

        #region UpdateUnitType methods
        
        public virtual void UpdateUnitType(UnitType unitType)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitTypeUpdate");            
                
                database.AddInParameter(dbCommand, "@UnitTypeId", DbType.Int64, unitType.UnitTypeId);
                database.AddInParameter(dbCommand, "@Name", DbType.String, unitType.Name);
                database.AddInParameter(dbCommand, "@Enable", DbType.Int64, unitType.Enable);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, unitType.Order);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateUnitTypeException, ex);
            }
        }
        
        #endregion

        #region DeleteUnitType methods
        public virtual void DeleteUnitType(long unitTypeId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitTypeDelete");
                
                database.AddInParameter(dbCommand, "@UnitTypeId", DbType.Int64, unitTypeId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteUnitTypeException, ex);
            }
        }

        #endregion

        #region GetUnitType methods
        
        public virtual UnitType GetUnitType(long unitTypeId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitTypeGet");
                
                database.AddInParameter(dbCommand, "@UnitTypeId", DbType.Int64, unitTypeId);
                
                UnitType unitType =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        unitType = CreateUnitTypeFromReader(reader);
                        reader.Close();
                    }
                }
                return unitType;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUnitTypeException, ex);
            }
        }
        
        #endregion

        #region GetUnitTypeList methods
        public virtual UnitTypeCollection GetUnitTypeList(UnitTypeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitTypeGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UnitTypeCollection unitTypeCollection = new UnitTypeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        UnitType unitType = CreateUnitTypeFromReader(reader);
                        unitTypeCollection.Add(unitType);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return unitTypeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUnitTypeListException, ex);
            }
        }
        
        public virtual UnitTypeCollection GetUnitTypeList(UnitTypeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUnitTypeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetUnitTypeList methods
        public virtual UnitTypeCollection GetUnitTypeListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUnitTypeGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UnitTypeCollection unitTypeCollection = new UnitTypeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        UnitType unitType = CreateUnitTypeFromReader(reader);
                        unitTypeCollection.Add(unitType);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return unitTypeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetUnitTypeListException, ex);
            }
        }
        
        public virtual UnitTypeCollection GetUnitTypeListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetUnitTypeListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}